import encrypt from "./encrypt.js"
export const apiBaseUrl = ''
// 获取签名
export const getSign = function getSign(options) {
	let appSecret = '20210219HAPPYANDRICH';
	options.machineNo = 'app';
	options.timestamp = new Date().getTime()
	let str = appSecret
	let obj = Object.keys(options).sort();
	for (let key in obj) {
		let a = obj[key]
		if (options[a] == undefined) {
			options[a] = ''
		}
		str = str + a + (options[a] || '')
	}
	str = str + appSecret
	let md5Str = encrypt.encrypt(str)
	md5Str = md5Str.toLowerCase()
	return md5Str;
}
export const getRequestUrl = function(){
	let requestUrl = "";
	if(!apiBaseUrl){
		let serverinfo = uni.getStorageSync("SERVERINFO");
		if(!serverinfo){
			uni.showToast({
				title:"请配置服务器信息"
			})
			return "";
		}
		let s = JSON.parse(serverinfo);
		requestUrl = "http://" + s.ip + ":" + s.port + "/";
		console.log(requestUrl+"-----")
		return requestUrl;
	}
}
const post = (method, data, callback, hideLoading) => {
	//如果调用接口不明确指明不显示loading
	if (!hideLoading) {
		uni.showLoading({
			//title: '加载中'
		});
	}
	let requestUrl = getRequestUrl();
	if(!requestUrl)return false;
	uni.request({
		url: requestUrl + method,
		data: data,
		method: 'POST',
		header: {
			'Content-Type': 'application/json'
		},
		success: (response) => {
			if (!hideLoading) {
				uni.hideLoading();
			}
			
			const result = response.data
			callback(result);
			
		},
		complete: () => {
			uni.hideLoading();
		},
		fail: (error) => {
			uni.hideLoading();
			if (error && error.response) {
				showError(error.response);
			}
		},
	});

}
const get = (method, data, callback, hideLoading) => {

	//如果调用接口不明确指明不显示loading
	if (!hideLoading) {
		uni.showLoading({
			//title: '加载中'
		});
	}
	let requestUrl = getRequestUrl();
	if(!requestUrl)return false;
	uni.request({
		url: requestUrl + method,
		data: data,
		method: 'GET',
		header: {
			'Content-Type': 'application/json'
		},
		success: (response) => {
			if (!hideLoading) {
				uni.hideLoading();
			}
			const result = response.data
			if (result.code == 0) {
				callback(result);
			} else if (result.code == 201) {
				callback(result);
			} else {

			}
		},
		complete: () => {
			uni.hideLoading();
		},
		fail: (error) => {
			uni.hideLoading();
		},
	});
}
export const submitLogin = (data, callback, hideLoading)=>{
	let param = {};
	param.code = data.name;
	param.password = encrypt.encrypt(data.password)
	const sign = getSign(param);

	param.sign = sign;

	post('restLogin', param, callback, true);
}
export const getInventoryPageList = (data,callback,hideLoading)=>{
	get('eqInventory/getPageList', data, callback, true);
	
}
export const getInventoryItemPageList = (data,callback,hideLoading)=>{
	get('eqInventoryItem/getPageList', data, callback, true);
	
}
export const checkInventory = (data,callback,hideLoading)=>{
	const sign = getSign(data);
	data.sign = sign;
	post('eqInventoryItem/check', data, callback, true);
	
}
export const handleInventory = (data,callback,hideLoading)=>{
	const sign = getSign(data);
	data.sign = sign;
	post('eqInventoryItem/handle', data, callback, true);
	
}